package model

import (
	"database/sql/driver"
	"encoding/json"
)

//type Stock struct {
//	BaseModel
//	Name    string
//	Address string
//}

type GoodsDetail struct {
	Goods int32
	Num   int32
}
type GoodsDetailList []GoodsDetail

// Scan 实现 sql.Scanner 接口，Scan 将 value 扫描至 Jsonb
func (g *GoodsDetailList) Scan(value interface{}) error {
	return json.Unmarshal(value.([]byte), &g)
}

// Value 实现 driver.Valuer 接口，Value 返回 json value
func (g GoodsDetailList) Value() (driver.Value, error) {
	return json.Marshal(g)
}

type Inventory struct {
	BaseModel
	GoodsID int32 `gorm:"type:int;index"`
	Stocks  int32 `gorm:"type:int"`
	Version int32 `gorm:"type:int"` // 分布式锁的乐观锁使用
}

type InventoryHistory struct {
	User   int32
	Goods  int32
	Order  int32
	Status int32 // 1.表示预扣库存, 幂等性 2.表示付款成功
}

// StocksSellDetail 录库存的扣减和归还情况
type StocksSellDetail struct {
	OrderSn string          `gorm:"type:varchar(200);index:idx_order_sn,unique;"`
	Status  int32           `gorm:"type:int"` // 1.代表已扣减 2.已归还
	Detail  GoodsDetailList `gorm:"type:varchar(200)"`
}

func (StocksSellDetail) TableName() string {
	return "stocksselldetail"
}
